
*-----------------------------------------------------------------
clear all
use "${data}/estimates_agg_naics_sales_s1_base_woparent.dta", clear

use "${data}/estimates_agg_naics_sales_s4_base_withparent.dta" 
**** ok este es el file correcto 

local heading "base_withparent"  /*"base"; "exclnomark"; "growth_base", "growth_exclnomark", "growth_base_gap", "growth_base_gap_woguoFE"; and then all robuntness are listed here*/
local varinterest "sales" /*sales; emp*/
*local timeframe `" "2005" "2006" "2007" "2008" "2009" "2010" "2011" "2012" "2013" "2014" "2015" "2016" "2017" "'  /* `" "2005" "2006" "2007" "2008" "2009" "2010" "2011" "2012" "2013" "2014" "2015" "2016" "2017" "' */
local timeframe `" "2016"  "'  /* `" "2005" "2006" "2007" "2008" "2009" "2010" "2011" "2012" "2013" "2014" "2015" "2016" "2017" "' */
include "code/firm_tfp_part2_reg_twowayFE_heading.do"
include "code/firm_tfp_part2_reg_twowayFE.do"



use year isocode type D_A D_P using "${data}/estimates_agg_naics_sales_s1_base_withparent.dta", clear
rename D_A D_A_new 
rename D_P D_P_new 
merge m:1 year isocode type using "${data}/estimates_agg_naics_sales_s4_base_withparent.dta", keepusing(D_A D_P)
drop _merge 


Figure A.2: Country effects: Alternative dependent variables

*global other_restr2 `" keep if sin_sales_ko!=. & sin_sales_ko!=0 & sin_emp_ko!=. & sin_emp_ko!=0 & sin_va_ko!=. & sin_va_ko!=0 "'



*** foreign and local ****
*Construct the market shares, Snn and Sfn, from MNC data. Snn=Xnn/Xn 
*--------------------------------------------------


use isocode sector1 sector year GO_usd_mnc GO_usd_nt using "${data}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear

**** CHECK 
*rename GO_usd_mnc GO_usd_mnc_new
*rename GO_usd_nt GO_usd_nt_new 
*merge 1:1  isocode sector1 sector year using "${data}/klems_oecd_unido_orbis_sales_emp_exp_0514.dta", keepusing(GO_usd_mnc GO_usd_nt)
*merge 1:1  isocode sector1 sector year using "${data}/klems_oecd_unido_orbis_sales_emp_exp_0830.dta", keepusing(GO_usd_mnc GO_usd_totnat)
*rename GO_usd_totnat GO_usd_nt
*merge 1:1  isocode sector1 sector year using "${data}/klems_oecd_unido_orbis_sales_emp_exp_0904.dta", keepusing(GO_usd_mnc GO_usd_nt)
*merge 1:1  isocode sector1 sector year using "${data}/klems_oecd_unido_orbis_sales_emp_exp_06152022.dta", keepusing(GO_usd_mnc GO_usd_nt)
*merge 1:1  isocode sector1 sector year using "${data}/klems_oecd_unido_orbis_sales_emp_exp_06202022.dta", keepusing(GO_usd_mnc GO_usd_nt)
*br if isocode=="JP" & year==2016
*order year isocode sector1 sector GO_usd_mnc_new GO_usd_mnc GO_usd_nt_new GO_usd_nt
**** CHECK 



merge 1:1 year isocode type using "${data}/estimates_agg_naics_sales_s1_base_woparent_decile_2_9.dta", keepusing(D_A*)
rename D_A D_A_decile_2_9
drop _merge
merge 1:1 year isocode type using "${data}/estimates_agg_naics_sales_s1_base_woparent_decile_3_8.dta", keepusing(D_A*)
rename D_A D_A_decile_3_8
drop _merge
merge 1:1 year isocode type using "${data}/estimates_agg_naics_sales_s1_base_woparent_decile_4_7.dta", keepusing(D_A*)
rename D_A D_A_decile_4_7
drop _merge
merge 1:1 year isocode type using "${data}/estimates_agg_naics_sales_s1_base_woparent_decile_5_6.dta", keepusing(D_A*)
rename D_A D_A_decile_5_6
drop _merge
merge 1:1 year isocode type using "${data}/estimates_agg_naics_sales_s1_base_woparent_above3ctry.dta", keepusing(D_A*)
rename D_A D_A_above3ctry
drop _merge
merge 1:1 year isocode type using "${data}/estimates_agg_naics_sales_s1_base_woparent_above5ctry.dta", keepusing(D_A*)
rename D_A D_A_above5ctry
drop _merge
merge 1:1 year isocode type using "${data}/estimates_agg_naics_sales_s4_base_woparent_above10ctry.dta", keepusing(D_A*)
rename D_A D_A_above10ctry
drop _merge


/*


display "estimates_`typeused'_`title'"
use "${output}/estimates_`typeused'_`title'.dta", clear
keep if year==`yy'
$condtype
merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
drop if D_A==. | (D_A==0 & isocode!="FR")
drop if `sample'  
tab type


use  "${data}/estimates_agg_naics_sales_s1_base_woparent_above5ctry.dta", clear
drop if isocode=="MX"
local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "P"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

gen D_A_above10ctry_phi=(-1)*D_A*(0.2) 
local LHS1 D_A_above10ctry_phi
local RHS ln_`grvar0'

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c1
display $se1

global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none)),  ylabel(, format(%5.1f) labsize(medlarge)) xlabel(-1(0.2)0, format(%5.1f) labsize(medlarge)) ///
	title("`title'", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

*/






*Inward and outward MP [ok ya se que voy hacer aca. No te muevas de esta parte]
*--------------------------------------------
*Mi pregunta es cual de los dos tiene outward. 
* Esta fue la data que usamos como outward.
* hay dos bases de datos. 
** klems_oecd_unido_orbis_sales_emp_exp.dta
** oecd_mnc_outward_rev4_restricted
** aggregates_tfp_lp_klems
** aggregates_tfp_lp_klems_agg
oecd_mnc_outward_rev4_restricted
* Estos son los codigos que se encargan de esto: 
include "code/aggregate_data_sales.do" /*Last file "${output}/klems_oecd_unido_orbis_sales_emp_exp.dta". Still mnc have to be incorporated*/
include "code/aggregate_lp_klems.do" /*"${output}/aggregates_tfp_lp_klems.dta"; "${output}/aggregates_tfp_lp_klems_agg.dta"; "${output}/aggregates_tfp_lp_klems_agg_growth.dta" */
 






use "${data}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear




*Total output by MNCs (OECD/Eurostat dataset)
*---------------------------------------------------------
use year isocode sector1 sector GO_usd_mnc GO_usd_mnc_out TURN_usd_mnc VA_usd_mnc EMPE_mnc ENT_mnc sales_mnc_orbis using "${data}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear
keep if year==2016
keep if sector1=="Total (TOT)"
keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isocode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 
collapse (sum) GO_usd_mnc ENT_mnc EMPE_mnc, by( isocode year)


merge 1:1 isocode year using "${data}/oecd_mnc_outward_rev4_restricted.dta", keepusing(*mnc_out_OECD)





*Total output by MNCs (OECD/Eurostat dataset)
*---------------------------------------------------------
use year isocode sector1 sector GO_usd_mnc GO_usd_mnc_out TURN_usd_mnc VA_usd_mnc EMPE_mnc ENT_mnc sales_mnc_orbis using "${data}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear
keep if year==2016
keep if sector1=="Total (TOT)"
keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isocode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 
collapse (sum) GO_usd_mnc ENT_mnc EMPE_mnc, by( isocode year)


merge 1:1 isocode year using "${data}/oecd_mnc_outward_rev4_restricted.dta", keepusing(*mnc_out_OECD)

